home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / bs941029.tgz / bbsx-941029.tar / bbsx / findpath.c < prev    next >
C/C++ Source or Header  |  1994-10-29  |  6KB  |  228 lines

  1. #ifndef __lint
  2. static char rcsid[] = "@(#) $Header: /home/dg1rtf/tcp/bbsx/RCS/findpath.c,v 1.2 1994/06/07 05:55:23 root Exp $";
  3. #endif
  4.  
  5. #define _HPUX_SOURCE
  6.  
  7. #include <ctype.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <pwd.h>
  15.  
  16. #include "bbs.hd"
  17. #include "bbs.h"
  18. #include "calc_crc.h"
  19. #include "callvalid.h"
  20.  
  21.  
  22.  
  23. #define HASH_SIZE 1009
  24.  
  25. struct node {
  26.   struct node *next;            /* Linked list pointer */
  27.   struct neighbor *neighbors;   /* List of neighbors */
  28.   char name[1];                 /* Node name */
  29. };
  30.  
  31. struct neighbor {
  32.   struct neighbor *next;        /* Linked list pointer */
  33.   struct node *node;            /* Pointer to node entry */
  34.   long date;                    /* Date of last usage */
  35. };
  36.  
  37. static const char *datafile = "findpath_data";
  38. static const char *tempfile = "findpath_temp";
  39. static long currdate;
  40. static long timeout;
  41. static struct node *nodes[HASH_SIZE];
  42. const struct cmdtable cmdtable[1];
  43.  
  44. /*---------------------------------------------------------------------------*/
  45.  
  46. static struct node *nodeptr(const char *name)
  47. {
  48.  
  49.   int hash;
  50.   struct node *node;
  51.  
  52.   for (node = nodes[hash = ((calc_crc_16(name) & 0x7fff) % HASH_SIZE)];
  53.        node && strcmp(node->name, name);
  54.        node = node->next)
  55.     ;
  56.   if (!node) {
  57.     node = malloc(sizeof(*node) + strlen(name));
  58.     node->next = nodes[hash];
  59.     node->neighbors = 0;
  60.     strcpy(node->name, name);
  61.     nodes[hash] = node;
  62.   }
  63.   return node;
  64. }
  65.  
  66. /*---------------------------------------------------------------------------*/
  67.  
  68. static void add_neighbor(struct node *node, struct node *neighbor, long date)
  69. {
  70.   struct neighbor *nb;
  71.  
  72.   for (nb = node->neighbors; nb && (nb->node != neighbor); nb = nb->next) ;
  73.   if (!nb) {
  74.     nb = malloc(sizeof(*nb));
  75.     nb->next = node->neighbors;
  76.     nb->node = neighbor;
  77.     nb->date = date;
  78.     node->neighbors = nb;
  79.     return;
  80.   }
  81.   if (nb->date < date) nb->date = date;
  82. }
  83.  
  84. /*---------------------------------------------------------------------------*/
  85.  
  86. static void add_route(const char *name1, const char *name2, long date)
  87. {
  88.   int cmp;
  89.  
  90.   if (date < timeout) return;
  91.   if (date > currdate) date = currdate;
  92.   cmp = strcmp(name1, name2);
  93.   if (cmp < 0)
  94.     add_neighbor(nodeptr(name1), nodeptr(name2), date);
  95.   else if (cmp > 0)
  96.     add_neighbor(nodeptr(name2), nodeptr(name1), date);
  97. }
  98.  
  99. /*---------------------------------------------------------------------------*/
  100.  
  101. static char *parseheader(char *line, long *date)
  102. {
  103.  
  104.   char *p, *q;
  105.   int tmp;
  106.   int year, month, day, hour, minute;
  107.   struct tm tm;
  108.  
  109.   if (sscanf(line, "R:%02d%02d%02d/%02d%02d", &year, &month, &day, &hour, &minute) != 5) return NULL;
  110.   if (day > 31) {
  111.     tmp = day;
  112.     day = year;
  113.     year = tmp;
  114.   }
  115.   if (year   < 80 || year   > 99 ||
  116.       month  <  1 || month  > 12 ||
  117.       day    <  1 || day    > 31 ||
  118.       hour   <  0 || hour   > 23 ||
  119.       minute <  0 || minute > 59) return NULL;
  120.   if (!(p = strchr(line, '@'))) return NULL;
  121.   p++;
  122.   while (*p == ':' || isspace(uchar(*p))) p++;
  123.   for (q = p; isalnum(uchar(*q)); q++)
  124.     if (*q >= 'A' && *q <= 'Z') *q = tolower(*q);
  125.   *q = 0;
  126.   if (!callvalid(p)) return NULL;
  127.   tm.tm_sec = 0;
  128.   tm.tm_min = minute;
  129.   tm.tm_hour = hour;
  130.   tm.tm_mday = day;
  131.   tm.tm_mon = month - 1;
  132.   tm.tm_year = year;
  133.   tm.tm_isdst = 0;
  134.   *date = mktime(&tm);
  135.   return p;
  136. }
  137.  
  138. /*---------------------------------------------------------------------------*/
  139.  
  140. int main(void)
  141. {
  142.  
  143.   FILE * fp;
  144.   char *host;
  145.   char filename[1024];
  146.   char line[1024];
  147.   char name1[1024];
  148.   char name2[1024];
  149.   char prev[1024];
  150.   int findex;
  151.   int i;
  152.   int lastmesg;
  153.   long date;
  154.   long metric;
  155.   struct index index;
  156.   struct neighbor *nb;
  157.   struct node *node;
  158.   struct passwd *pw;
  159.  
  160.   if (getuid()) {
  161.     pw = getpwnam(bbsadm);
  162.     if(getuid() != pw->pw_uid) {
  163.       perror("permission denied");
  164.       return 1;
  165.     }
  166.   }    
  167.  
  168.   if (chdir(WRKDIR)) {
  169.     mkdir(WRKDIR, 0755);
  170.     if(chdir(WRKDIR)) halt();
  171.   }  
  172.  
  173.   read_config();
  174.  
  175.   putenv("TZ=UTC0");
  176.   time(&currdate);
  177.   timeout = currdate - 365L * 24L * 60L * 60L;
  178.  
  179.   if ((findex = open(INDEXFILE, O_RDONLY, 0644)) < 0) return 1;
  180.   if (lseek(findex, -sizeof(struct index), SEEK_END) < 0) return 1;
  181.   if (read(findex, &index, sizeof(struct index)) != sizeof(struct index)) return 1;
  182.   close(findex);
  183.  
  184.   lastmesg = 0;
  185.   fp = fopen(datafile, "r");
  186.   if (fp) {
  187.     if (fgets(line, sizeof(line), fp)) lastmesg = atoi(line);
  188.     while (fgets(line, sizeof(line), fp) && sscanf(line, "%s%s%ld", name1, name2, &date) == 3)
  189.       add_route(name1, name2, date);
  190.     fclose(fp);
  191.   }
  192.  
  193.   i = lastmesg - 20;
  194.   if (i < 1) i = 1;
  195.   lastmesg = 0;
  196.   for (; i <= index.mesg; i++) {
  197.     strcpy(filename,getfilename(i));
  198.     if (!(fp = fopen(filename, "r"))) continue;
  199.     strcpy(prev, myhostname);
  200.     while (fgets(line, sizeof(line), fp) && (host = parseheader(line, &date))) {
  201.       add_route(prev, host, date);
  202.       strcpy(prev, host);
  203.       lastmesg = i;
  204.     }
  205.     fclose(fp);
  206.   }
  207.  
  208.   fp = fopen(tempfile, "w");
  209.   if (fp) fprintf(fp, "%d\n", lastmesg);
  210.   for (i = 0; i < HASH_SIZE; i++)
  211.     for (node = nodes[i]; node; node = node->next)
  212.       for (nb = node->neighbors; nb; nb = nb->next) {
  213.     if (fp) fprintf(fp, "%s %s %ld\n", node->name, nb->node->name, nb->date);
  214.     if (strcmp(node->name, myhostname) && strcmp(nb->node->name, myhostname)) {
  215.       metric = 500L * ((currdate - nb->date) / (30L * 24L * 60L * 60L) + 1L);
  216.       printf("%s\t%s(%ld)\n", node->name, nb->node->name, metric);
  217.       printf("%s\t%s(%ld)\n", nb->node->name, node->name, metric);
  218.     }
  219.       }
  220.   if (fp) {
  221.     fclose(fp);
  222.     rename(tempfile, datafile);
  223.   }
  224.  
  225.   return 0;
  226. }
  227.  
  228.